<?php
/*
 * $Id: JSONSerializerTest.class.php 148 2009-12-16 15:55:26Z namba0219 $
 * =============================================================================
 * Barge - Web application framework on PHP5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace barge\json;

use \UnitTestCase;

use barge\utils\ArrayList;
use barge\utils\Date;
use barge\utils\HashMap;
use barge\utils\IndexOutOfBoundsException;
use barge\utils\Timestamp;
use barge\utils\IllegalAccessException;
use barge\json\JSONSerializer;

/**
 * @see UnitTestCase
 */
class JSONSerializerTest extends UnitTestCase
{
	/**
	 * @see JSONSerializer#serialize
	 */
	public function testSerialize()
	{
		$serializer = new JSONSerializer();
		$this->assertTrue($serializer->serialize(1) === '1');
		$this->assertTrue($serializer->serialize("1") === "\"1\"");
		$this->assertTrue($serializer->serialize("\n") === "\"\\n\"");
		$this->assertTrue($serializer->serialize("\r") === "\"\\r\"");
		$this->assertTrue($serializer->serialize(true) === 'true');
		$this->assertTrue($serializer->serialize(false) === 'false');
		$this->assertTrue($serializer->serialize(1.1) === '1.1');
		$this->assertTrue($serializer->serialize((double)1.1) === '1.1');
		$this->assertTrue($serializer->serialize(-1.1) === '-1.1');
		$this->assertTrue($serializer->serialize(new ArrayList(1, 2, 3)) === '[1,2,3]');
		$this->assertTrue($serializer->serialize(array(1, 2, 3)) === '[1,2,3]');
		$this->assertTrue($serializer->serialize(array('apple'=>1, 'banana'=>1.1, 'orange'=>true))
			 === '{"apple":1,"banana":1.1,"orange":true}');
		$this->assertTrue($serializer->serialize(new Model(1, 'Bob'))
			 === '{"userId":1,"name":"Bob"}');

		$serializer->setDateFormat('Y-m-d');
		$this->assertTrue($serializer->serialize(new Date()) === '"'.date('Y-m-d').'"');
		$serializer->setTimestampForamt('Y-m-d H:i');
		$this->assertTrue($serializer->serialize(new Timestamp()) === '"'.date('Y-m-d H:i').'"');

	}

}

class Model
{
	private $user_id;
	private $name;
	private $birthday;
	private $entryOn;
	public $gender;
	public function __construct($id, $name)
	{
		$this->user_id = $id;
		$this->name = $name;
	}
	public function getUserId()
	{
		return $this->user_id;
	}
	public function getName()
	{
		return $this->name;
	}
	protected function getEntryOn()
	{
		return $this->entryOn;
	}

}
?>